<?php
/**
 * Created by PhpStorm.
 * User: LOPO
 * Date: 2020/4/15
 * Time: 14:17
 */
namespace App\HttpController\Api;

use App\Lib\ClassArr;
use App\HttpController\Api\Base;

/** 文件上传 -视频-图片 逻辑
 * Class Upload
 * @package App\HttpController\Api
 */
class Upload extends Base
{

    public function file(){
        $request = $this->request();
        $files = $request->getSwooleRequest()->files;
        $filess = $request->getUploadedFiles();
        $files1 = $request->getUploadedFile('video');

        print_r($filess);
        print_r($files);
        print_r($files1);
        print_r($files1->getStream());
        $types = array_keys($files);
        $type = $types[0];
        if (empty($type)){
            return $this->writeJson(400,"上传文件不合法");
        }
//        print_r($type);
//        if ($type ==="image"){
//            $obj ="\App\Lib\Upload\Image";
//        }elseif ($type ==="video"){
//            $obj ="\App\Lib\Upload\Video";
//        }
        //php反射机制
        try{
//            $obj = new $obj($request);
            $classObj = new ClassArr();
            $classStats = $classObj->uploadClassStat();
            $uploadObj = $classObj->initClass($type,$classStats,[$request,$type]);
            $file = $uploadObj->upload();
        }catch (\Exception $e){
            return $this->writeJson(400,$e->getMessage(),[]);
        }
        if (empty($file)){
            return $this->writeJson(400,"上传失败",[]);
        }
        $data = [
            'url'=>$file
        ];
        return $this->writeJson(200,"OK",$data);
//        $videos = $request->getUploadedFile("file");
//        $flag = $videos->moveTo("/test_tmp/1.png");
//        $data =[
//            'url'=>'/1.png',
//            'flag'=>$flag
//        ];
//        if($flag){
//            return $this->writeJson(200,'OK',$data);
//        }
//        return $this->writeJson(400,'OK',$data);
    }

}